package com.wwwarehouse.resource_center.fragment.resource_efficiency_evaluation;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.util.Log;
import android.view.View;

import com.alibaba.fastjson.JSON;
import com.github.lzyzsd.jsbridge.BridgeHandler;
import com.github.lzyzsd.jsbridge.BridgeWebView;
import com.github.lzyzsd.jsbridge.CallBackFunction;
import com.wwwarehouse.common.activity.base.BaseActivity;
import com.wwwarehouse.common.constant.Constant;
import com.wwwarehouse.common.custom_widget.state_layout.StateLayout;
import com.wwwarehouse.common.eventbus_event.LogoutEvent;
import com.wwwarehouse.common.tools.LogUtils;
import com.wwwarehouse.resource_center.R;
import com.wwwarehouse.resource_center.bean.goods.WebDataBean;

import de.greenrobot.event.EventBus;

/**
 * @author Joseph on 2017/11/1.
 *         <p/>
 *         资源效率横屏webview
 */

public class ResourceWebViewActivity extends BaseActivity  {

    protected BridgeWebView mWebView = null;
    protected String mData="";
    private StateLayout mStateLayout;
    private String Url = null;
    private String index ;
    private  String mBaseUrl = "";
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.resource_web_view_activity);
        Intent intent = getIntent();
        if (intent != null) {
            Bundle bundle = intent.getExtras();
            if (bundle!=null){
                WebDataBean dataBean  =  bundle.getParcelable("data");
                mData= JSON.toJSONString(dataBean);
                Url = dataBean.getUrl();
                index = dataBean.getIndex();
                Log.d("Activity_url",dataBean.getUrl()+"index:"+dataBean.getIndex());
            }
        }
        mWebView = findView(R.id.bridge_web_view);
        mStateLayout = findView(R.id.loading);
        mStateLayout.showProgressView(false);
        String env = sp.getValue(Constant.sp_Env, sp.getValue(Constant.sp_EnvDefault, Constant.ENV_DEFAULT));
        if (Constant.ENV_DEV.equals(env)) {
            mBaseUrl = Constant.URL_WEB_BASE_DEV;
        } else if (Constant.ENV_CIT.equals(env)) {
            mBaseUrl = Constant.URL_WEB_BASE_CIT;
        } else if (Constant.ENV_SIT.equals(env)) {
            mBaseUrl = "";
        } else if (Constant.ENV_UAT.equals(env)) {
            mBaseUrl = Constant.URL_WEB_BASE_UAT;
        } else if (Constant.ENV_RELEASE.equals(env)) {
            mBaseUrl = Constant.URL_WEB_BASE_RELEASE;
        }

        String newUrl = mBaseUrl + Url;
        Log.d("newUrl",newUrl);
        mWebView.loadUrl(newUrl);
        Log.d("getBusinessActivity",mData);
        registJsMethod();
    }

//    @Override
//    public void onCreate(@Nullable Bundle savedInstanceState, @Nullable PersistableBundle persistentState) {
//        super.onCreate(savedInstanceState, persistentState);
//        setContentView(R.layout.fragment_common_bridge_web_view);
//
//    }
    private void registJsMethod() {
        //Js获取请求的参数
        mWebView.callHandler("getData", mData, new CallBackFunction() {
            @Override
            public void onCallBack(String data) {
                LogUtils.showInfoLog(data);
            }
        });
        mWebView.callHandler("getToken", sp.getValue(Constant.sp_Token), new CallBackFunction() {
            @Override
            public void onCallBack(String data) {

            }
        });
        mWebView.callHandler("getBusinessParam", mData, new CallBackFunction() {
            @Override
            public void onCallBack(String data) {

            }
        });

            //退出
        mWebView.registerHandler("didExitFullScreenAction", new BridgeHandler() {
            public void handler(String data, CallBackFunction function) {
                    Intent intent = new Intent();
                    intent.putExtra("data", index);
                    setResult(1, intent);
                   ResourceWebViewActivity.this.finish();
            }
        });
        //显示progressDialog
        mWebView.registerHandler("showProgress", new BridgeHandler() {
            @Override
            public void handler(String data, CallBackFunction function) {
//                showProgressDialog("正在加载");
            }
        });

        mWebView.registerHandler("hideProgress", new BridgeHandler() {
            @Override
            public void handler(String data, CallBackFunction function) {
                if (mWebView.getVisibility() != View.VISIBLE) {
                    mWebView.setVisibility(View.VISIBLE);
                }
                if (mStateLayout.getVisibility() != View.INVISIBLE) {
                    mStateLayout.setVisibility(View.INVISIBLE);
                    mStateLayout.showContentView();
                }
                dismissProgress();
            }
        });

        //跳转到登录页
        mWebView.registerHandler("gotoLogin", new BridgeHandler() {
            @Override
            public void handler(String data, CallBackFunction function) {
                EventBus.getDefault().post(new LogoutEvent("logout"));
            }
        });

        //获取请求错误信息
        mWebView.registerHandler("showErrorMsg", new BridgeHandler() {
            @Override
            public void handler(String data, CallBackFunction function) {
                mWebView.setVisibility(View.INVISIBLE);
                mStateLayout.setVisibility(View.VISIBLE);
                mStateLayout.showSystemView(data, false);
                mStateLayout.setSystemListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        mWebView.reload();
                    }
                });
                LogUtils.showErrLog("showErrorMsg");
            }
        });

        //数据为空的界面
        mWebView.registerHandler("dataEmptyOperation", new BridgeHandler() {
            @Override
            public void handler(String data, CallBackFunction function) {
                mStateLayout.setVisibility(View.VISIBLE);
                mStateLayout.showEmptyView(data, false);
                mStateLayout.setEmptyListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        mWebView.reload();
                    }
                });
                LogUtils.showErrLog("showEmptyView");
            }
        });
    }
}
